package com.lvhaosb.newpneumonia.spider;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lvhaosb.newpneumonia.POJO.vo.DomesticCityEpidemicInfo;
import com.lvhaosb.newpneumonia.POJO.vo.DomesticEpidemicInfo;
import com.lvhaosb.newpneumonia.POJO.vo.ForeignEpidemicInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.scheduler.BloomFilterDuplicateRemover;
import us.codecraft.webmagic.scheduler.QueueScheduler;

import java.util.ArrayList;
import java.util.List;

@Component
public class SpiderProcessor implements PageProcessor {
    private String url = "https://voice.baidu.com/act/newpneumonia/newpneumonia/";

    @Override
    public void process(Page page) {
        //解析页面获取数据
        String now = page.getHtml().css("script#captain-config").toString();
        now=now.substring(now.indexOf("captain-config")+16,now.indexOf("</script>"));
        JSONObject json = JSONObject.parseObject(now);

        //国内疫情数据
        JSONObject summaryDataIn = json.getJSONArray("component").getJSONObject(0).getJSONObject("summaryDataIn");
        DomesticEpidemicInfo domesticEpidemicInfo = new DomesticEpidemicInfo("time",summaryDataIn.getInteger("curConfirm"),summaryDataIn.getInteger("asymptomatic"),summaryDataIn.getInteger("unconfirmed"),summaryDataIn.getInteger("icu"),summaryDataIn.getInteger("confirmed"),summaryDataIn.getInteger("overseasInput"),summaryDataIn.getInteger("cured"),summaryDataIn.getInteger("died"));
        page.putField("domesticEpidemicInfo", domesticEpidemicInfo);
        /**
         * test
         * 查看一下从百度爬来的数据
         */
        //System.out.println(summaryDataIn);

        //国内各省疫情数据
        JSONArray caseList = json.getJSONArray("component").getJSONObject(0).getJSONArray("caseList");
        List<DomesticCityEpidemicInfo> domesticCityEpidemicInfoList = new ArrayList<>();
        for(int index = 0; index < caseList.size(); index++){
            JSONObject jsonObject = caseList.getJSONObject(index);
            DomesticCityEpidemicInfo domesticCityEpidemicInfo = new DomesticCityEpidemicInfo(jsonObject.getString("area"),"time",jsonObject.getInteger("confirmedRelative"),jsonObject.getInteger("curConfirm"),jsonObject.getInteger("died"),jsonObject.getInteger("confirmed"),jsonObject.getInteger("crued"));
            domesticCityEpidemicInfoList.add(domesticCityEpidemicInfo);
            /**
             * 查看cured字段
             */
            //System.out.println("cured:" + jsonObject.getInteger("crued"));
            //System.out.println(domesticCityEpidemicInfo);
        }
        page.putField("domesticCityEpidemicInfoList", domesticCityEpidemicInfoList);
        /**
         * test
         * 查看一下从百度爬来的数据，发现cured 拼错了，百度也会错
         */
        //System.out.println(caseList);

        //国外各国家疫情数据
        JSONArray caseOutsideList = json.getJSONArray("component").getJSONObject(0).getJSONArray("caseOutsideList");
        List<ForeignEpidemicInfo> foreignEpidemicInfoList = new ArrayList<>();
        for(int index = 0; index < caseOutsideList.size(); index++){
            JSONObject jsonObject = caseOutsideList.getJSONObject(index);
            ForeignEpidemicInfo foreignEpidemicInfo = new ForeignEpidemicInfo("time",jsonObject.getString("area"),jsonObject.getInteger("confirmedRelative"),jsonObject.getInteger("curConfirm"),jsonObject.getInteger("died"),jsonObject.getInteger("confirmed"),jsonObject.getInteger("crued"));
            foreignEpidemicInfoList.add(foreignEpidemicInfo);
        }
        page.putField("foreignEpidemicInfoList", foreignEpidemicInfoList);
        /**
         * test
         * 查看一下从百度爬来的数据，发现cured 拼错了，百度也会错
         */
        //System.out.println(caseOutsideList);

    }

    private Site site = Site.me()
            .setCharset("utf8")//设置编码
            .setTimeOut(10*1000)//设置超时时间
            .setRetrySleepTime(3000)//设置重试间隔
            .setRetryTimes(3);//设置重试次数

    @Override
    public Site getSite() {
        return site;
    }

    @Autowired
    private SpiderPipeline spiderPipeline;

    //任务启动后等待多久执行该方法initialDelay
    //每隔多久执行该方法fixedDelay
    //@Scheduled(initialDelay = 1000, fixedDelay = 60*5*1000)
    public void process() {
        Spider.create(new SpiderProcessor())
                .addUrl(url)
                .setScheduler(new QueueScheduler().setDuplicateRemover(new BloomFilterDuplicateRemover(10000)))
                .thread(10)
                .addPipeline(this.spiderPipeline)
                .run();
    }
}
